//
//  LTQWelcomeViewController.swift
//  WeiBoProject
//
//  Created by MAC on 16/3/1.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class LTQWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backImageView
        
        setupUI()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //      网络上加载图片
        
        let url = NSURL(string: LTQUserViewModel.shareInstance.userModel?.profile_image_url ?? "")
        
        userPhotoImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))

    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //    动画效果
    private func startAnimation(){
        
        // 1. 更改约束 -> 改变位置
        // snp_updateConstraints 更新已经设置过的约束
        // multiplier 属性是只读属性，创建之后，不允许修改
        /**
        使用自动布局开发，有一个原则：
        - 所有`使用约束`设置位置的控件，不要再设置 `frame`
        
        * 原因：自动布局系统会根据设置的约束，自动计算控件的 frame
        * 在 layoutSubviews 函数中设置 frame
        * 如果程序员主动修改 frame，会引起 自动布局系统计算错误！
        
        - 工作原理：当有一个运行循环启动，自动布局系统，会`收集`所有的约束变化
        - 在运行循环结束前，调用 layoutSubviews 函数`统一`设置 frame
        - 如果希望某些约束提前更新！使用 `layoutIfNeeded` 函数让自动布局系统，提前更新当前收集到的约束变化
        */
        
        self.userPhotoImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-UIScreen.mainScreen().bounds.size.height + 170 )
        }
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.nameLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(kSwitchRootViewControllerNotification, object: nil)
                })
                
        }
        
        
    }
    
    
    func setupUI (){
        
        view.addSubview(userPhotoImageView)
        view.addSubview(nameLabel)
        
        userPhotoImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-140)
            make.centerX.equalTo(view.snp_centerX)
            
            make.size.equalTo(CGSizeMake(90, 90))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userPhotoImageView.snp_bottom).offset(10)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        
    }

    
///  背景图
    private lazy var backImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "ad_background")
        return img
        }()
///    圆型用户头像
    private lazy var userPhotoImageView: UIImageView = {
        
        let img = UIImageView()
        img.image = UIImage(named: "avatar_default_big")
//        圆型图需要切割
        img.layer.cornerRadius = 45
        img.layer.masksToBounds = true
        
//        有外边框
        img.layer.borderColor = UIColor.grayColor().CGColor
        img.layer.borderWidth = 2
        return img
        
    }()
///     用户名  欢迎回来
    private lazy var nameLabel:UILabel = {
       
        let label = UILabel()
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        let name = LTQUserViewModel.shareInstance.userModel?.screen_name ?? ""
        
        label.text = "\(name)欢迎归来"
        
        return label
        
    }()
    
    

}
